package com.huadi.firematerialident.service;

import com.huadi.firematerialident.dao.AdminDao;
import com.huadi.firematerialident.dao.AdminDaoImpl;
import com.huadi.firematerialident.dao.UserDaoImpl;
import com.huadi.firematerialident.po.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService{
    @Autowired
    AdminDaoImpl adminDao;
    @Autowired
    UserDaoImpl userDao;
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public int addAdmin(Admin admin) {
        return adminDao.addAdmin(admin);
    }

    @Override
    public int deleteAdminByID(String adminID) {
        return adminDao.deleteAdminByID(adminID);
    }

    @Override
    public int updateAdmin(Admin admin) {
        return adminDao.updateAdmin(admin);
    }

    @Override
    public List<Map<String,Object>> selectAdminByID(String adminID) {
        return adminDao.selectAdminByID(adminID);
    }

    @Override
    public List<Map<String,Object>> getAllAdmin() {
        return adminDao.getAllAdmin();
    }

    @Override
    public String getAdminID() {
        String AdminCount = userDao.getUsername();
        List<Map<String, Object>> list = jdbcTemplate.queryForList("select AdminID,AdminCount from admin");
        for (int i = 0; i < list.size(); i++) {
            if (AdminCount.equals(list.get(i).get("AdminCount"))) {
                return (String) list.get(i).get("AdminID");
            }
        }
        return null;
    }

    @Override
    public Admin selectAdminByID_class(String id){
        List<Map<String ,Object>> list = adminDao.selectAdminByID(id);
        Admin admin = new Admin();
        admin.setAdminID((String) list.get(0).get("AdminID"));
        admin.setAdminCount((String) list.get(0).get("AdminCount"));
        admin.setAdminPassword((String) list.get(0).get("AdminPassword"));
        admin.setAdminName((String) list.get(0).get("AdminName"));
        admin.setAdminSex((String) list.get(0).get("AdminSex"));
        admin.setAdminBirth((LocalDate) list.get(0).get("AdminBirth"));
        admin.setAdminPhone((String) list.get(0).get("AdminPhone"));
        admin.setLogicalAddDel((boolean) list.get(0).get("LogicalAddDel"));
        admin.setLastModification((LocalDateTime) list.get(0).get("LastModification"));
        return admin;
    }    
}
